package com.sz.biz.common.contract.service.impl;

import com.google.common.collect.Lists;
import com.sz.biz.common.constants.CommCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.common.contract.entity.ContContractor;
import com.sz.biz.common.contract.service.ContTypeEnum;
import com.sz.biz.common.contract.service.ContContractorService;
import com.sz.biz.common.contract.dto.ContContractorDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.biz.logistics.constants.ErrorCodes;
import com.sz.common.core.service.PrincipalLogUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Package: com.sz.biz.common.contract.service.impl<br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: yanhong.xiao.<br>
 * Date: 2017-07-19 16:14:00.
 */
@Service
public class ContContractorServiceImpl extends AbstractService implements ContContractorService {

    @Override
    protected String getMapperNamespace() {
        return "com.sz.biz.common.contract.entity.ContContractorMapper";
    }

    @Override
    public void save(ContContractorDto contContractorDto) {

    }

    /**
     * 批量保存合同方
     *
     * @param contContractorDtoList
     */
    @Override
    public void batchSave(List<ContContractorDto> contContractorDtoList) {
        List<ContContractor> contractorList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(contContractorDtoList)) {
            if (checkContractorDtos(contContractorDtoList)) {
                for (ContContractorDto dto :
                        contContractorDtoList) {
                    ContContractor contractor = new ContContractor();
                    EntityUtils.copyPropertiesIgnoreNull(dto, contractor);
                    contractorList.add(contractor);
                }
            }
            //批量保存
            dao.batchInsert(getSqlName("insertSelective"), contractorList);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("ADD_CONT_CONTRACTOR", contractorList);
            PrincipalLogUtils.addOperationLog(ModuleNames.CONT, ModuleNames.CONT_CONTRACTOR, UserActions.ADD, "添加合同中的合同方", dataBackup);
        }

    }

    /**
     * 根据类型，批量删除对应的resIds下的合同方
     *
     * @param contTypeEnum
     * @param relIds
     */
    @Override
    public void batchDeleteByRelIds(ContTypeEnum contTypeEnum, List<Integer> relIds) {
        Map<String, Object> map = new HashedMap();
        map.put("type", ContTypeEnum.getIntegerCode(contTypeEnum));
        if (CollectionUtils.isNotEmpty(relIds)) {
            map.put("relIds", relIds);
        }
        dao.delete(getSqlName("batchDeleteByRelIds"), map);
    }

    /**
     * 根据relId获取合同方
     *
     * @param contTypeEnum
     * @param relId
     * @return
     */
    @Override
    public List<ContContractor> getContractorsByRelId(ContTypeEnum contTypeEnum, Integer relId) {
        Map<String, Object> map = new HashedMap();
        map.put("type", ContTypeEnum.getIntegerCode(contTypeEnum));
        map.put("relId", relId);
        List<ContContractor> contContractorList = dao.findForList(getSqlName("getContractorsByRelId"), map, ContContractor.class);
        return contContractorList;
    }

    @Override
    public Map<String,ContContractorDto> getContractorDtosByRelId(ContTypeEnum contTypeEnum, Integer relId) {
        List<ContContractor> contContractorList =  getContractorsByRelId(contTypeEnum,relId);
        Map<String,ContContractorDto> map = new HashedMap();
        if (CollectionUtils.isNotEmpty(contContractorList)){
            contContractorList.forEach(contContractor -> {
                ContContractorDto dto = new ContContractorDto();
                EntityUtils.copyPropertiesIgnoreNull(contContractor,dto);
                //甲方
                if (dto.getContractorType()==CommCodes.CONT_CONTRACTOR_FIRST){
                    map.put(CommCodes.CONT_CONTRACTOR_FIRST_KEY,dto);
                }
                if (dto.getContractorType()==CommCodes.CONT_CONTRACTOR_SECOND){
                    map.put(CommCodes.CONT_CONTRACTOR_SECOND_KEY,dto);
                }
            });
        }

        return map;
    }

    @Override
    public List<ContContractor> getContractorsByRelIds(ContTypeEnum contTypeEnum, List<Integer> relIds) {
        Map<String, Object> map = new HashedMap();
        map.put("type", ContTypeEnum.getIntegerCode(contTypeEnum));
        map.put("relIds", relIds);
        List<ContContractor> contContractorList = dao.findForList(getSqlName("getContractorsByRelIds"), map, ContContractor.class);
        return contContractorList;
    }

    @Override
    public Map<Object, Map<String, ContContractorDto>> getContractorDtos(List<ContContractor> contractorList) {
        Map<Object, Map<String, ContContractorDto>> result=new HashedMap();
        if (CollectionUtils.isNotEmpty(contractorList)){
            contractorList.forEach(contContractor -> {
                Map<String, ContContractorDto> map = new HashedMap();
                ContContractorDto dto = new ContContractorDto();
                EntityUtils.copyPropertiesIgnoreNull(contContractor,dto);
                //甲方
                if (dto.getContractorType()==CommCodes.CONT_CONTRACTOR_FIRST){
                    map.put(CommCodes.CONT_CONTRACTOR_FIRST_KEY,dto);
                }
                if (dto.getContractorType()==CommCodes.CONT_CONTRACTOR_SECOND){
                    map.put(CommCodes.CONT_CONTRACTOR_SECOND_KEY,dto);
                }
                result.put(contContractor.getRelId(),map);
            });
        }
        return result;
    }


    private boolean checkContractorDtos(List<ContContractorDto> contContractorDtoList) {
        if (CollectionUtils.isNotEmpty(contContractorDtoList)) {
            for (ContContractorDto dto :
                    contContractorDtoList) {
                boolean typeFlag = true;
                ContTypeEnum typeEnum = ContTypeEnum.CONT_TEMPLATE;
                if (null == dto.getType()) {
                    typeFlag = false;
                } else {
                    typeEnum = ContTypeEnum.valueOfEnum(String.valueOf(dto.getType()));
                    if (null == typeEnum) {
                        typeFlag = false;
                    }
                }
                if (!typeFlag) {
                    throw Exceptions.bizException(ErrorCodes. ERROR_CONT_CONTRACTOR_TYPE);
                }

                if (null == dto.getRelId() || dto.getRelId() == 0) {
                    if (typeEnum == ContTypeEnum.CONT_TEMPLATE) {
                        throw Exceptions.bizException(ErrorCodes.ERROR_CONT_TEMPLATE_NOT_EXISTS);
                    }
                    if (typeEnum == ContTypeEnum.CONT_CONTRACTOR) {
                        throw Exceptions.bizException(ErrorCodes.  ERROR_CONT_CONTRACT_INVALID);


                    }
                }
            }

        }

        return true;
    }
}
